﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SegmentTree
{
    public class SegmentTree<E>
    {

        private E[] tree;
        private E[] data;
        private Merger<E> merger;

        public SegmentTree(E[] arr, Merger<E> merger)
        {
            this.merger = merger;

            data = new E[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                data[i] = arr[i];

            tree = new E[4 * arr.Length];
            buildSegmentTree(0, 0, arr.Length - 1);
        }

        // 在treeIndex的位置创建表示区间[l...r]的线段树
        private void buildSegmentTree(int treeIndex, int l, int r)
        {

            if (l == r)
            {
                tree[treeIndex] = data[l];
                return;
            }

            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);

            // int mid = (l + r) / 2;
            int mid = l + (r - l) / 2;
            buildSegmentTree(leftTreeIndex, l, mid);
            buildSegmentTree(rightTreeIndex, mid + 1, r);

            tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
        }

        public int getSize()
        {
            return data.Length;
        }

        public E get(int index)
        {
            if (index < 0 || index >= data.Length)
                throw new Exception("Index is illegal.");
            return data[index];
        }

        // 返回完全二叉树的数组表示中，一个索引所表示的元素的左孩子节点的索引
        private int leftChild(int index)
        {
            return 2 * index + 1;
        }

        // 返回完全二叉树的数组表示中，一个索引所表示的元素的右孩子节点的索引
        private int rightChild(int index)
        {
            return 2 * index + 2;
        }



        // 返回区间[queryL, queryR]的值
        public E query(int queryL, int queryR)
        {

            if (queryL < 0 || queryL >= data.Length ||
                    queryR < 0 || queryR >= data.Length || queryL > queryR)
                throw new Exception("Index is illegal.");

            return query(0, 0, data.Length - 1, queryL, queryR);
        }

        // 在以treeIndex为根的线段树中[l...r]的范围里，搜索区间[queryL...queryR]的值
        private E query(int treeIndex, int l, int r, int queryL, int queryR)
        {

            if (l == queryL && r == queryR)
                return tree[treeIndex];

            int mid = l + (r - l) / 2;
            // treeIndex的节点分为[l...mid]和[mid+1...r]两部分

            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);
            if (queryL >= mid + 1)
                return query(rightTreeIndex, mid + 1, r, queryL, queryR);
            else if (queryR <= mid)
                return query(leftTreeIndex, l, mid, queryL, queryR);

            E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
            E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
            return merger.merge(leftResult, rightResult);
        }

        // 将index位置的值，更新为e
        public void set(int index, E e)
        {

            if (index < 0 || index >= data.Length)
                throw new Exception("Index is illegal");

            data[index] = e;
            set(0, 0, data.Length - 1, index, e);
        }

        // 在以treeIndex为根的线段树中更新index的值为e
        private void set(int treeIndex, int l, int r, int index, E e)
        {

            if (l == r)
            {
                tree[treeIndex] = e;
                return;
            }

            int mid = l + (r - l) / 2;
            // treeIndex的节点分为[l...mid]和[mid+1...r]两部分

            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);
            if (index >= mid + 1)
                set(rightTreeIndex, mid + 1, r, index, e);
            else // index <= mid
                set(leftTreeIndex, l, mid, index, e);

            tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
        }


        public override string ToString()
        {
            StringBuilder res = new StringBuilder();
            res.Append('[');
            for (int i = 0; i < tree.Length; i++)
            {
                if (tree[i] != null)
                    res.Append(tree[i]);
                else
                    res.Append("null");

                if (i != tree.Length - 1)
                    res.Append(", ");
            }
            res.Append(']');
            return res.ToString();
        }
    }
}
